package cse3311.calendarapp;

import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.PowerManager;
import android.support.v4.app.NotificationCompat;

public class NotificationReciever extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		PowerManager pm = (PowerManager) context
				.getSystemService(Context.POWER_SERVICE);
		PowerManager.WakeLock wl = pm.newWakeLock(
				PowerManager.PARTIAL_WAKE_LOCK, "YOUR TAG");
		// Acquire the lock
		wl.acquire();
		Bundle extras = intent.getExtras();
		String title = (String) extras.get("Event Name");
		title = title != null ? title : "Event Notifiaction";
		String text = (String) extras.get("Notes");
		text = text != null ? text : title;

		NotificationCompat.Builder note = new NotificationCompat.Builder(
				context).setContentTitle(title).setContentText(text)
				.setSmallIcon(R.drawable.ic_launcher);

		NotificationManager noteMgr = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		noteMgr.notify(1, note.build());

		// Release the lock
		wl.release();

	}

	public void setEventNotification(Context context, long time,
			String eventName, String notes, int rCode) {
		AlarmManager am = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE);

		Intent intent = new Intent(context, NotificationReciever.class);
		intent.putExtra("Event Name", eventName);
		intent.putExtra("Notes", notes);
		PendingIntent pi = PendingIntent.getBroadcast(context, rCode, intent,
				PendingIntent.FLAG_CANCEL_CURRENT);

		am.set(AlarmManager.RTC_WAKEUP, time, pi);
	}

	public void cancelEventNotification(Context context, int rCode) {
		Intent intent = new Intent(context, NotificationReciever.class);
		PendingIntent pi = PendingIntent.getBroadcast(context, rCode, intent,
				PendingIntent.FLAG_CANCEL_CURRENT);
		AlarmManager alarmManager = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE);
		alarmManager.cancel(pi);
	}

}
